package controller

import (
	"github.com/cn-ygf/go-admin/internal/action"
	"github.com/cn-ygf/go-admin/internal/modular/system/service"
	"github.com/cn-ygf/go-admin/internal/modular/system/vo"
	"github.com/cn-ygf/go-admin/pkg/utils"
	"github.com/kataras/iris/v12"
	"github.com/kataras/iris/v12/mvc"
)

// MenuController 菜单管理
type MenuController struct {
	Ctx iris.Context
}

// GetList 查询菜单列表
func (c *MenuController) GetList() mvc.Result {
	if !hasPermi(c.Ctx, "system:menu:list") {
		return nil
	}
	menuName := c.Ctx.URLParam("menuName")
	status := c.Ctx.URLParam("status")
	list, err := service.MenuService.GetList(menuName, status)
	if err != nil {
		systemLog.Errorln(err.Error())
		return action.ServerError()
	}
	return action.Data(list)
}

// GetBy 根据id获取菜单信息
func (c *MenuController) GetBy(id int64) mvc.Result {
	if !hasPermi(c.Ctx, "system:menu:query") {
		return nil
	}
	menu, err := service.MenuService.GetByID(id)
	if err != nil {
		systemLog.Errorln(err.Error())
		return action.ServerError()
	}
	return action.Data(&vo.SysMenu{
		MenuID:    menu.MenuID,
		MenuName:  menu.MenuName,
		ParentID:  menu.ParentID,
		OrderNum:  menu.OrderNum,
		Path:      menu.Path,
		Component: menu.Component,
		Query:     menu.Query,
		IsFrame:   menu.IsFrame,
		IsCache:   menu.IsCache,
		MenuType:  menu.MenuType,
		Visible:   menu.Visible,
		Status:    menu.Status,
		Perms:     menu.Perms,
		Icon:      menu.Icon,
		Remark:    menu.Remark,
		CreatedAt: menu.CreatedAt,
		UpdatedAt: menu.UpdatedAt,
	})
}

// Put 修改菜单
func (c *MenuController) Put(req vo.EditMenuReq) mvc.Result {
	if !hasPermi(c.Ctx, "system:menu:edit") {
		return nil
	}
	if !service.MenuService.CheckMenuNameUnique(req.MenuID, req.MenuName) {
		return action.Error("修改菜单'" + req.MenuName + "'失败，菜单名称已存在")
	}
	if req.IsFrame == 0 && !utils.IsHttp(req.Path) {
		return action.Error("修改菜单'" + req.MenuName + "'失败，地址必须以http(s)://开头")
	}
	if req.MenuID == req.ParentID {
		return action.Error("修改菜单'" + req.MenuName + "'失败，上级菜单不能选择自己")
	}
	req.UpdateBy = getCurrentUser(c.Ctx).UserName
	err := service.MenuService.Update(&req)
	if err != nil {
		systemLog.Errorln(err.Error())
		return action.ServerError()
	}
	return action.OK()
}

// Post 添加菜单
func (c *MenuController) Post(req vo.AddMenuReq) mvc.Result {
	if !hasPermi(c.Ctx, "system:menu:add") {
		return nil
	}
	if !service.MenuService.CheckMenuNameUnique(0, req.MenuName) {
		return action.Error("新增菜单'" + req.MenuName + "'失败，菜单名称已存在")
	}
	if req.IsFrame == 0 && !utils.IsHttp(req.Path) {
		return action.Error("新增菜单'" + req.MenuName + "'失败，地址必须以http(s)://开头")
	}
	req.CreateBy = getCurrentUser(c.Ctx).UserName
	err := service.MenuService.Add(&req)
	if err != nil {
		systemLog.Errorln(err.Error())
		return action.ServerError()
	}
	return action.OK()
}

// DeleteBy 删除菜单
func (c *MenuController) DeleteBy(id int64) mvc.Result {
	if !hasPermi(c.Ctx, "system:menu:remove") {
		return nil
	}
	if service.MenuService.HasChildByMenuId(id) {
		return action.Error("存在子菜单，不允许删除")
	}
	if service.MenuService.CheckMenuExistRole(id) {
		return action.Error("菜单已分配，不允许删除")
	}

	err := service.MenuService.Delete([]int64{id})
	if err != nil {
		systemLog.Errorln(err.Error())
		return action.ServerError()
	}
	return action.OK()
}

// GetTreeSelect 菜单树形数据
func (c *MenuController) GetTreeSelect() mvc.Result {
	root, err := service.MenuService.GetMenuTree()
	if err != nil {
		return action.ServerError()
	}
	menus, err := service.MenuService.BuildTree(root)
	if err != nil {
		return action.ServerError()
	}
	return action.Data(menus)
}

// GetRoleMenuTreeSelectBy 角色关联权限树形
func (c *MenuController) GetRoleMenuTreeSelectBy(roleId int64) mvc.Result {
	root, err := service.MenuService.GetMenuTree()
	if err != nil {
		return action.ServerError()
	}
	menus, err := service.MenuService.BuildTree(root)
	if err != nil {
		return action.ServerError()
	}
	return action.Data(map[string]any{
		"menus":       menus,
		"checkedKeys": service.RoleService.GetPermIdsByRoleId(roleId),
	})
}
